package com.android.nearshop.app.activity.common;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.provider.Settings;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.view.ViewStub;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.android.nearshop.R;
import com.android.nearshop.app.MyApplication;
import com.android.nearshop.app.widget.AdvView;
import com.android.nearshop.app.widget.LoadableView.IdisplayNetErrAndRefreshView;
import com.android.nearshop.app.widget.TitleBar;
import com.android.nearshop.business.cache.DataCacheManager;
import com.android.nearshop.io.protocol.in.BannerAction.ADV_TYPE;
import com.android.nearshop.io.protocol.out.Adv;
import com.android.nearshop.util.NotifyListener;

/*******************************************************
 * @author : zhaohua
 * @version: 2012-08-14
 * @see:     继承自这个Activity的界面有统一的标题栏
 * @Copyright: copyrights reserved by personal 2007-2012
*******************************************************/
public class TemplateActivity extends BaseActivity implements IdisplayNetErrAndRefreshView
{
	/**标题栏 */
	protected TitleBar mTitleBar;
	/**内容容器*/
	private LinearLayout mContainer;
	/** 广告栏*/
	private ViewStub advStub;
	private View  advLay;
	private AdvView adv_img;
	private ImageView adv_close;
	
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
	}
	
	@Override
	protected void onResume()
	{	    
	    super.onResume();
	    showAdvView(( getAdvType() != ADV_TYPE.NONE && MyApplication.getContext().isAdvOn()));
	}
	
	public void setContentView(View view)
	{
		initLayout(view);
	}
	
	@Override
	public void setContentView(int layoutResID)
	{
		View content = getLayoutInflater().inflate(layoutResID, null);
		initLayout(content);
	}

	private void initLayout(View content) {
		// 设置模板
		super.setContentView(R.layout.activity_template);
		// 初始化模拟成员
		initComponents();		
		// 将内容放入容器
		
		mContainer.addView(content,new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
		// 设置模板的标题
		setupTitleBar(mTitleBar);
	}
	
	/**
	 * 初始化三大块：头部的标题栏，中间的内容容器
	 */
	private void initComponents()
	{
	    mTitleBar = (TitleBar)findViewById(R.id.titleBar);	
	    mContainer = (LinearLayout)findViewById(R.id.container);	   	    
	    showAdvView((getAdvType()!= ADV_TYPE.NONE && MyApplication.getContext().isAdvOn()));
	}
	
	/** 初始化广告组件*/
	private void initAdvViews()
	{
	    advLay = findViewById(R.id.advLay);
        adv_img = (AdvView)findViewById(R.id.adv_img);
        adv_close = (ImageView)findViewById(R.id.adv_close);
        adv_close.setOnClickListener(new OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                advLay.setVisibility(View.GONE);
                MyApplication.getContext().setAdvOn(false);
                sendBroadcast(new Intent("ad.close"));
            }
        });
	}
	
	/** 是否显示广告视图*/
	private void showAdvView(boolean visible)
	{
	    advStub = (ViewStub)findViewById(R.id.advStub);
	    if(advStub == null)
	    {
	        // 已经初始化了
	        advLay.setVisibility( visible ? View.VISIBLE : View.GONE );
	    }else
	    {
	        // 还没有，只有显示时，才初始化
	        if(visible)
	        {
	            advStub.setVisibility(View.VISIBLE);
	            initAdvViews();
	        }
	    }
	    if(visible)
	    {
	        if(getAdvType() == ADV_TYPE.SUB)
	        {
	            DataCacheManager.getInstance().getSubAdv(advListener,false);
	        }
	    }
	}
	
	/** 设置标题栏*/
	protected void setupTitleBar(TitleBar titleBar){}
	
	/** 广告是否展示，可由子类去决定，默认显示其它广告*/
	protected ADV_TYPE getAdvType()
	{
	    return ADV_TYPE.SUB;
	}
	
	private NotifyListener<Adv> advListener = new NotifyListener<Adv>()
    {
        @Override
        public void onSucceed(Adv result)
        {
            adv_img.bindData(result);
        }
        @Override
        public void onError(int resultCode)
        {
            advLay.setVisibility(View.GONE);
        }
    };

    @Override
    public void refreshView()
    {
        
    }

    @Override
    public void showNoNetworkDialog()
    {
        showDialog("网络设置提示", "网络连接不可用,是否进行设置?", "取消", "设置", 
                new DialogInterface.OnClickListener()
        {           
            @Override
            public void onClick(DialogInterface dialog, int which)
            {
              startActivity(new Intent(Settings.ACTION_WIRELESS_SETTINGS));
            }
        });
    }
}
